import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const routes = [
  {
    path: '/',
    name: 'index',
    component: () => import('../views/index/index.vue'),
    meta: { title: '申请分享员' },
  },
]

const router = new Router({
  mode: 'history',
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { x: 0, y: 0 }
    }
  },
})

// 设置页面标题
router.beforeEach((to, from, next) => {
  document.title = to.meta.title || ''
  let url = window.location.href
  const extensionRegex = /\.txt$/ // 正则表达式匹配.txt结尾
  if (!extensionRegex.test(url)) {
    next()
  } else {
    window.location.href = url
  }
})

export default router